package cn.hejiang.user.conrtoller;

import cn.hejiang.basic.exception.BusinessException;
import cn.hejiang.basic.util.AjaxResult;
import cn.hejiang.user.dto.LoginDto;
import cn.hejiang.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;

    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> map =  loginService.accountLogin(loginDto);
            // 返回token字符串和登录人的信息
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，稍后重试！！");
        }

    }

    // 微信登录流程
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody Map<String, String> params){
        try {
            return loginService.wechat(params);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，稍后重试！！");
        }

    }

    // 微信登录流程
    @PostMapping("/binder/wechat")
    public AjaxResult binder(@RequestBody Map<String, String> params){
        try {
            Map<String,Object> map =  loginService.binder(params);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，稍后重试！！");
        }

    }
}
